import ajaxhelper from './ajaxhelper.js';
var app = getApp();
var checkLogin = {
  // 检查有没有登录，第二个参数是回调函数 参数是token
  login(ctx,successFun){
    //----------------------------------------------------------------------- 登录搬到onshow里面
    var that = ctx;
    var token_checkout = +new Date();
    var Token_date = wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').newData;
    var Token = (wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token) || '';
    if (!Token || (!Token_date) || (token_checkout > Token_date)) {
      // 登陆
      wx.login({
        success: function (res) {
          console.log(res)
          var md = { code: res.code };
          wx.getUserInfo({
            success: function (res) {
              console.log(res)
              var userInfo = res.userInfo;
              // that.setData({
              //   userName: userInfo.nickName
              // })
              wx.setStorageSync('getUserInfo', userInfo)
              var nickName = userInfo.nickName;
              var headImageUrl = userInfo.avatarUrl;
              var params = {
                code: md.code,
                logo: headImageUrl,
                nickname: encodeURIComponent(nickName)
              }

              ajaxhelper.get(app.globalData.frontJSHost + 'weixinmini/login', params, this, function (res) {
                if (res.result) {
                  var token = res.data.token;
                  var isBind = res.data && res.data.isBind;
                  if (token) {
                    var tokens = token.trim().split('=');
                    if (tokens && tokens.length > 1) {
                      token = tokens[1];
                    }
                  }
                  // wx.setStorageSync('Token', token);
                  // that.getData();//获取数据
                  // 回调
                  if (successFun && Object.prototype.toString.call(successFun) === '[object Function]'){
                    successFun.apply(ctx,[token]);
                  }
                  var newData = +new Date();
                  wx.setStorageSync('Token_date', {
                    Token: token,
                    newData: newData + 30 * 24 * 60 * 60 * 1000,
                    isBind: isBind
                  });
                }


              });
            }, fail: function () {
              var params = {
                code: md.code,
                // logo: '',
                // nickname: ''
              }
              ajaxhelper.get(app.globalData.frontJSHost + 'weixinmini/login', params, this, function (res) {
                if (res.result) {
                  var token = res.data.token;
                  var isBind = res.data && res.data.isBind;
                  if (token) {
                    var tokens = token.trim().split('=');
                    if (tokens && tokens.length > 1) {
                      token = tokens[1];
                    }
                  }
                  // wx.setStorageSync('Token', token);
                  // that.scanCodeRequ(groupId, subCode, scanCode);
                  // that.getData();//获取数据
                  // 回调
                  if (successFun && Object.prototype.toString.call(successFun) === '[object Function]') {
                    successFun.apply(ctx, [token]);
                  }
                  var newData = +new Date();
                  wx.setStorageSync('Token_date', {
                    Token: token,
                    newData: newData + 30 * 24 * 60 * 60 * 1000,
                    isBind:isBind
                  });
                }
              })
            }
          })
        }
      });
    } else {
      //var token = (wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token)||'';
      // that.scanCodeRequ(groupId, subCode, scanCode);
      // that.getData();
      // 回调
      if (successFun && Object.prototype.toString.call(successFun) === '[object Function]') {
        successFun.apply(ctx, [Token]);
      }
    }
  },
}

export default checkLogin;
